﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using WindowsLive.Writer.Api;
using Microsoft.Win32;

namespace ScottIsAFool
{
    public static class WriterUtils
    {
        public static bool ReplaceText(IPostInfo postInfo, string value, PostItems item)
        {
            PropertyInfo[] propInfos = postInfo.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in propInfos)
            {
                if (pi.Name == item.ToString() && pi.CanWrite)
                {
                    pi.SetValue(postInfo, value, null);
                    return true;
                }
            }
            return false;
        }

        public static Blog[] ConfiguredBlogs()
        {
            List<Blog> result = new List<Blog>();
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows Live\Writer\Weblogs", false);
            foreach (string r in key.GetSubKeyNames())
            {
                RegistryKey blog = key.OpenSubKey(r, false);
                string blogname = blog.GetValue("BlogName").ToString();
                string blogid = blog.GetValue("BlogId").ToString();
                string homepage = blog.GetValue("HomepageUrl").ToString();
                string servicename = blog.GetValue("ServiceName").ToString();
                Blog cblog = new Blog(r, blogname, blogid, homepage, servicename);
                result.Add(cblog);
            }

            return result.ToArray();
        }

        public static string[] ParseString(string source)
        {
            char[] c = { ',' };
            string[] result = source.Split(c, StringSplitOptions.RemoveEmptyEntries);
            return result;
        }

        public static string ParseStringArray(string[] source)
        {
            string result = string.Empty;
            foreach (string s in source)
            {
                result += s + ",";
            }
            return result;
        }

    }

}
